﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Daneas.Utility.Universals
{
    public class MD5
    {
        public static string SetMD5String(string str)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] date = System.Text.Encoding.Default.GetBytes(str);
            byte[] result = md5.ComputeHash(date);
            string store = "";
            for (int i = 0; i < result.Length; i++)
            {
                //16/32位加密X/X2
                store += result[i].ToString("X");
            }
            store = store.Replace("-", "");
            return store;
        }

        /// <summary>
        /// 小写16位加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetMD516Lower(string str)
        {
            return InitMD5Str(str, false).ToLower();
        }

        /// <summary>
        /// 大写16位加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetMD516Upper(string str)
        {
            return InitMD5Str(str, false).ToUpper();
        }

        /// <summary>
        /// 小写32位加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetMD532Lower(string str)
        {
            return InitMD5Str(str, true).ToLower();
        }

        /// <summary>
        /// 大写32位加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetMD532Upper(string str)
        {
            return InitMD5Str(str, false).ToUpper();
        }

        private static string InitMD5Str(string str, bool Is32)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] date = System.Text.Encoding.Default.GetBytes(str);
            byte[] result = md5.ComputeHash(date);
            string store = "";
            for (int i = 0; i < result.Length; i++)
            {
                //16/32位加密X/X2
                if (!Is32)
                {
                    store += result[i].ToString("X");
                }
                else
                {
                    store += result[i].ToString("X2");
                }
            }
            store = store.Replace("-", "");
            return store;
        }
    }
}
